package fr.cephb.operonj.core.db.database;

import com.sleepycat.bind.tuple.ByteBinding;
import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.Transaction;

import fr.cephb.operonj.core.je.PrimaryDB;

public abstract class AbstractSetDB<K> extends PrimaryDB<K,Byte>
	{
	private TupleBinding<Byte> valueBinding=new ByteBinding();
	private DatabaseEntry TRUE=new DatabaseEntry();
	
	protected AbstractSetDB(Environment env, Transaction txn,
			String databaseName, DatabaseConfig dbConfig)
			throws DatabaseException
		{
		super(env, txn, databaseName, dbConfig);
		ByteBinding.byteToEntry((byte)'1', TRUE);
		}

	public boolean add(Transaction txn,K key) throws DatabaseException
		{
		OperationStatus status= getDatabase().putNoOverwrite(txn,
				keyToEntry(key),TRUE);
		if(status==OperationStatus.KEYEXIST) return false;
		if(status==OperationStatus.SUCCESS) return true;
		throw new RuntimeException("Cannot insert status:"+status);
		}
	
	@Override
	public abstract TupleBinding<K> getKeyBinding();

	@Override
	public TupleBinding<Byte> getValueBinding()
		{
		return valueBinding;
		}

	}
